﻿using System;
using System.Configuration;
using System.Globalization;
using System.Web.UI.WebControls;
using System.Data;
using System.Web;

namespace Shuba.Web.master
{
    public partial class summary_monthly: basePage
    {
        bool IsView = false;
        string msg = "";

        private void BindData()
        {
            string sYear = ddlYear.SelectedValue;
            string sMonth = ddlMonth.SelectedValue;
            
            string RegionalID, ClusterID, UnitID;

            if (ddlRegion.SelectedValue == "ALL") RegionalID = "%"; else RegionalID = ddlRegion.SelectedValue;
            if (ddlCluster.SelectedValue == "ALL") ClusterID = "%"; else ClusterID = ddlCluster.SelectedValue;
            if (ddlUnit.SelectedValue == "ALL") UnitID = "%"; else UnitID = ddlUnit.SelectedValue;

            Shuba.Business.Reports _Report = new Shuba.Business.Reports();
            DataSet ds = _Report.RPT_MONTHLY_PERFORMANCE(RegionalID, ClusterID, UnitID, sMonth, sYear);
            //DataView source = ds.Tables[0].DefaultView;

            gvList.DataSource = ds;
            gvList.DataBind();

            if (gvList.Rows.Count > 0) btnExcel.Enabled = true; else btnExcel.Enabled = false;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            string acl = GetUserAccess("SUMMARY_MONTHLY");

            IsView = IsUserView(acl);

            if (!IsView)
            {
                msg = this.GetGlobalResourceObject("Messages", "NoPrivilege").ToString();
                AlertAndRedirect(msg, ResolveUrl(ConfigurationManager.AppSettings["default_url"]));
            }

            if (!IsPostBack)
            {
                InitYearList();
                InitMonthList();

                InitRegionList();
                InitClusterList(ddlRegion.SelectedValue);
                InitUnitList(ddlRegion.SelectedValue, ddlCluster.SelectedValue);

                ddlYear.SelectedIndex = ddlYear.Items.IndexOf(ddlYear.Items.FindByValue(ProcessingDate().Year.ToString()));
                ddlMonth.SelectedIndex = ddlMonth.Items.IndexOf(ddlMonth.Items.FindByValue(ProcessingDate().Month.ToString()));

                LabelTitle.Text = GetPageTitle("SUMMARY_MONTHLY");
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            //System.Threading.Thread.Sleep(3000);
            BindData();
        }

        protected void btnExcel_Click(object sender, EventArgs e)
        {
            string RegionCode = ddlRegion.SelectedValue;
            string RegionName = ddlRegion.SelectedItem.Text;
            string ClusterCode = ddlCluster.SelectedValue;
            string ClusterName = ddlCluster.SelectedItem.Text;
            string UnitCode = ddlUnit.SelectedValue;
            string UnitName = ddlUnit.SelectedItem.Text;
            string Bulan = ddlMonth.SelectedValue;
            string Tahun = ddlYear.SelectedValue;

            string sParam = "'" + RegionCode + "','" + RegionName + "','" + ClusterCode + "','" + ClusterName + "','" + UnitCode + "','" + UnitName + "','" + Tahun + "','" + Bulan + "'"; 
            //BindData();
            //GridViewExportUtil.Export("rpt_summary_monthly_" + ConfigurationManager.AppSettings["app.code"] + "_" + ProcessingDate().ToString("ddMMMyyyy") + ".xls", gvList);
            System.Web.UI.ScriptManager.RegisterStartupScript(Page, this.GetType(), "Print", "PrintThis(" + sParam + ");", true);
        }

        protected void InitYearList()
        {
            Int32 NextYear = Convert.ToInt32(ProcessingDate().Year.ToString()) + 4;
            Int32 LastYear = Convert.ToInt32(ProcessingDate().Year.ToString()) - 1;

            for (var n = LastYear; n < NextYear; n++)
            {
                ddlYear.Items.Add(n.ToString());
            }
        }

        protected void InitMonthList()
        {
            var MyMonth = new DateTimeFormatInfo();

            for (int i = 1; i < 13; i++)
            {
                ListItem list = new ListItem();
                list.Text = MyMonth.GetMonthName(i);
                list.Value = i.ToString();
                ddlMonth.Items.Add(list);
            }
        }
        
        private void InitUnitList(string RegionID, string ClusterID)
        {
            var bl = new Shuba.Business.Branch();

            if (RegionID == "ALL") RegionID = "%";
            if (ClusterID == "ALL") ClusterID = "%";

            ddlUnit.DataTextField = "BranchName";
            ddlUnit.DataValueField = "BranchID";
            ddlUnit.DataSource = bl.GetListByRegionAndCluster(RegionID, ClusterID, HttpContext.Current.User.Identity.Name, ConfigurationManager.AppSettings["app.code"]);
            ddlUnit.DataBind();

            if (ddlUnit.Items.Count > 1)
            {
                ListItem ListAll = new ListItem();
                ListAll.Value = "ALL";
                ListAll.Text = " = ALL UNIT = ";
                ddlUnit.Items.Insert(0, ListAll);
            }
        }

        private void InitClusterList(string RegionalID)
        {
            var _Cluster = new Shuba.Business.Cluster();

            if (RegionalID == "ALL") RegionalID = "%";

            ddlCluster.DataTextField = "ClusterName";
            ddlCluster.DataValueField = "ClusterID";
            ddlCluster.DataSource = _Cluster.GetListByRegion(RegionalID, Session["UnitID"].ToString(), HttpContext.Current.User.Identity.Name, ConfigurationManager.AppSettings["app.code"]);
            ddlCluster.DataBind();
            
            if (ddlCluster.Items.Count > 1)
            {
                ListItem ListAll = new ListItem();
                ListAll.Value = "ALL";
                ListAll.Text = " = ALL CLUSTER = ";
                ddlCluster.Items.Insert(0, ListAll);
            }
        }

        private void InitRegionList()
        {
            var _Region = new Shuba.Business.Regional();

            ddlRegion.DataTextField = "RegionalName";
            ddlRegion.DataValueField = "RegionalID";
            ddlRegion.DataSource = _Region.GetList("%", Session["UnitID"].ToString(), HttpContext.Current.User.Identity.Name, ConfigurationManager.AppSettings["app.code"]);
            ddlRegion.DataBind();

            if (ddlRegion.Items.Count > 1)
            {
                ListItem ListAll = new ListItem();
                ListAll.Value = "ALL";
                ListAll.Text = " = ALL REGION = ";

                ddlRegion.Items.Insert(0, ListAll);
            }
        }

        protected void ddlRegion_SelectedIndexChanged(object sender, EventArgs e)
        {
            InitClusterList(ddlRegion.SelectedValue);
            InitUnitList(ddlRegion.SelectedValue, ddlCluster.SelectedValue);
        }

        protected void ddlCluster_SelectedIndexChanged(object sender, EventArgs e)
        {
            InitUnitList(ddlRegion.SelectedValue, ddlCluster.SelectedValue);
        }

        protected void ddlUnit_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlUnit.Items.Count == 0)
            {
                btnSearch.Enabled = false;
                btnExcel.Enabled = false;
            }
            else
            {
                btnSearch.Enabled = true;
                btnExcel.Enabled = true;
            }
        }
    }
 }

